﻿///LGraphics.as 该类为统一的类可以创建各种图案
//vesion 1.0
//author 夏天的树人
package org.summerTree.graphics
{
	import flash.geom.Point;
	import flash.display.Shape;
	public class FlashGraphics extends Base implements IGraphics
	{

		public static  const RAD:String="rad";
		public static  const ANGLE:String="angle";
		public static  const ANGLE2:String="angle2";
		public function FlashGraphics()
		{
			
		}

		public function clone():*
		{
			return new FlashGraphics();
		}
		//创建箭头
		/*radius 轴径
		 *fillColor 填充颜色
		 *alpha 透明度
		 */
		public function CreatArrow(radius:Number,fillColor:uint=0xffffff,alpha:Number=1):void
		{
			if (radius < 0)
			{
				return;
			}

			var list:Array=new Array  ;
			this.graphics.lineStyle(1,color);
			this.graphics.beginFill(fillColor,alpha);

			for (var i:int=-1; i < 2; i++)
			{
				var point:Point=Point.polar(radius,i * 2 * Math.PI / 3);
				list.push(point);
			}
			var len:int=list.length;
			this.graphics.moveTo(0,0);
			for (var j:int=0; j < 3; j++)
			{
				drawPoint(list[j]);
			}
			this.graphics.lineTo(0,0);
			this.graphics.endFill();
		}
		
		//创建正弦曲线图案
		//y=a*sin(b*x);
		public function CreatSinLine(swing:Number,wavelengh:Number,X_length:Number,amount:int,startX:Number=0,startY:Number=0):void
		{
			this.graphics.lineStyle(1,color);
			this.graphics.moveTo(startX,startY);
			var W_len:int=int(360 / wavelengh);//波长180度

			if (amount <= 0)
			{
				return;
			}
			for (var i:int=0; i <= W_len * amount; i++)
			{
				var posX:Number=i * X_length / W_len;
				var posY:Number=swing * Math.sin(- i * wavelengh * Math.PI / 180);
				this.graphics.lineTo(posX,posY);
			}
		}
		
		//创建多边形
		/*radius 半径
		 *n  边数
		 *fillColor 填充颜色
		 *alpha 透明度填充透明度
		*/
		public function CreatPolygon(radius:Number,n:int,fillColor:uint=0xffffff,alpha:Number=1):void
		{
			this.graphics.lineStyle(1,color);
			var angle:Number=2 * Math.PI / n;
			var tempArray:Array=new Array  ;
			for (var i:int=0; i < n; i++)
			{
				var px:Number=radius * Math.cos(i * angle);
				var py:Number=radius * Math.sin(i * angle);
				var point:Point=new Point(px,py);
				tempArray.push(point);
			}
			var len:int=tempArray.length;
			this.graphics.beginFill(fillColor,alpha);
			this.graphics.moveTo(tempArray[0].x,tempArray[0].y);
			for (var j:int=1; j < len; j++)
			{
				drawPoint(tempArray[j]);
			}
			this.graphics.lineTo(tempArray[0].x,tempArray[0].y);
			this.graphics.endFill();

		}
		
		//创建2D的螺旋线
		/*r 为半径
		 *angleType 度制类型 弧度还是角度
		 */
		//n，次数
		public function creatHelixLine(r:Number,n:int,angleType:String="rad"):void
		{
			this.graphics.lineStyle(1,color);
			this.graphics.moveTo(0,0);
			var angle:Number;
			if (angleType == "rad")
			{
				var rad:Number=n * Math.PI / 180;
				angle=rad;
			}
			else if (angleType == "angle")
			{
				rad=2 * Math.PI / n;
				angle=n;
			}
			else
			{
				return;
			}
			for (var j:Number=0; j <= 360; j++)
			{
				var len:Number=r * j * rad;
				var point:Point=Point.polar(len,j * angle);
				drawPoint(point);
			}
		}
		
		 //绘制扇形,以角度为制
		 /*radius 半径
		 *startAngle 开始角
		 *endAngle   结束角
		 *fillcolor 填充颜色
		 *alpha 透明度
		 */
		public function drawSector(radius:Number,startAngle:Number,endAngle:Number,fillcolor:uint,alpha:Number=1):void
		{
			this.graphics.lineStyle(1,color);
			this.graphics.moveTo(0,0);
			this.graphics.beginFill(fillcolor,alpha);
			for (var i:int=startAngle; i <= endAngle; i++)
			{
				this.graphics.lineTo(radius * Math.cos(- i * Math.PI / 180),radius * Math.sin(- i * Math.PI / 180));
			}
			this.graphics.endFill();
			this.graphics.lineTo(0,0);
		}
		//创建图案
		public function CreatStarLine(radius:Number):void
		{
			this.graphics.moveTo(0,0);
			this.graphics.lineStyle(1,this.color);
			for (var i:int=0; i <= 360; i++)
			{
				var px:Number=radius * Math.pow(Math.cos(i * Math.PI / 180),3);
				var py:Number=radius * Math.pow(Math.sin(i * Math.PI / 180),3);
				if (i == 0)
				{
					this.graphics.moveTo(px,py);
					drawPoint(px,py);
				}
				else
				{
					drawPoint(px,py);
				}
			}
		}
		//创建星形
		//参数第一个意思长轴，第二个短轴，n为边数
		public function CreatStar(long_radius:Number,short_radius:Number,n:int=5,fillcolor:uint=0xffffff,alpha:Number=1):void
		{
			this.graphics.lineStyle(1,color);
			if (n <= 0)
			{
				return;
			}
			var angle:Number=Math.PI / n;
			var tempArray:Array=new Array  ;
			for (var i:int=0; i < n * 2; i++)
			{
				if (i % 2 == 0)
				{
					var l_px:Number=long_radius * Math.cos(i * angle);
					var l_py:Number=long_radius * Math.sin(i * angle);
					tempArray.push(new Point(l_px,l_py));
				}
				else
				{
					var s_px:Number=short_radius * Math.cos(i * angle);
					var s_py:Number=short_radius * Math.sin(i * angle);
					tempArray.push(new Point(s_px,s_py));
				}
			}
			this.graphics.beginFill(fillcolor,alpha);
			this.graphics.moveTo(tempArray[0].x,tempArray[0].y);
			for (var j:int=0; j < n * 2; j++)
			{

				drawPoint(tempArray[j]);
			}
			this.graphics.lineTo(tempArray[0].x,tempArray[0].y);
			this.graphics.endFill();

		}
		
		//创建齿轮
        

		public function CreatGearWheel(long_radius:Number,short_radius:Number,n:int=5,fillcolor:uint=0xffffff,alpha:Number=1):void
		{

			this.graphics.lineStyle(1,color);
			if (n <= 0)
			{
				return;
			}
			var angle:Number=Math.PI / n;
			var tempArray:Array=new Array  ;
			var key:Boolean=true;
			this.graphics.beginFill(fillcolor,alpha);
			var l_px:Number;
			var l_py:Number;
			var s_px:Number;
			var s_py:Number;

			for (var i:int=0; i < n * 2; i++)
			{
				if (i % 2 == 0)
				{
					l_px=long_radius * Math.cos(i * angle);
					l_py=long_radius * Math.sin(i * angle);
					tempArray.push(new Point(l_px,l_py));

					if (key)
					{
						key=false;
						s_px=short_radius * Math.cos(i * angle);
						s_py=short_radius * Math.sin(i * angle);
						tempArray.push(new Point(s_px,s_py));
					}
				}
				else
				{
					s_px=short_radius * Math.cos(i * angle);
					s_py=short_radius * Math.sin(i * angle);
					tempArray.push(new Point(s_px,s_py));
					if (! key)
					{
						key=true;
						l_px=long_radius * Math.cos(i * angle);
						l_py=long_radius * Math.sin(i * angle);
						tempArray.push(new Point(l_px,l_py));
					}
				}
			}
			this.graphics.beginFill(fillcolor,alpha);
			this.graphics.moveTo(tempArray[0].x,tempArray[0].y);
			for (var j:int=0; j < tempArray.length; j++)
			{

				drawPoint(tempArray[j]);
			}
			this.graphics.lineTo(tempArray[0].x,tempArray[0].y);
			this.graphics.endFill();

			var len:Number=long_radius > short_radius?long_radius:short_radius;
			this.graphics.drawCircle(0,0,len * 0.3);
			this.graphics.drawCircle(0,0,len * 0.1);
		}
		
		
		
		/*long_radius 外环长轴半径
		 *short_radius 内环短轴半径
		 *fillColor 填充颜色
		 *alpha 透明度填充透明度
		 */
		public function CreatCirque(long_radius:Number,short_radius:Number,fillColor:uint=0xffffff,alpha:Number=1):void
		{
			this.graphics.lineStyle(1,color);
			this.graphics.beginFill(fillColor,alpha);
			this.graphics.drawCircle(0,0,long_radius);
			this.graphics.drawCircle(0,0,short_radius);
			this.graphics.endFill();
		}
		
		
		
		//创建菱形
		/*radius 长度
		 *
		 *fillColor 填充颜色
		 *alpha 透明度填充透明度
		 */
		public function CreatDiamond(radius:Number,fillColor:uint=0xffffff,alpha:Number=1):void
		{
			this.graphics.lineStyle(1,color);

			var tempArray:Array=[[radius,0],[0,- radius / 2],[- radius,0],[0,radius / 2]];
			this.graphics.beginFill(fillColor,alpha);
			this.graphics.moveTo(tempArray[0][0],tempArray[0][1]);
			for (var j:int=1; j < 4; j++)
			{
				drawPoint(new Point(tempArray[j][0],tempArray[j][1]));
			}
			this.graphics.lineTo(tempArray[0][0],tempArray[0][1]);
			this.graphics.endFill();
		}
		
		//创建2D的玫瑰线
		/*@ radius 半径
		 *@ num 花瓣数
		 *@ type 角度和弧度选择
		 *@ lens 角度
		 *@ count 360度值
		*/
		public function CreatRose(radius:Number,num:Number,type:String="rad",lens:int=1,count:int=360):void
		{
			this.graphics.lineStyle(0,color);
			var rad:Number;
			if (type == Rose.RAD)
			{
				rad=lens * Math.PI / 180;//角度  
			}
			else if (type == Rose.ANGLE)
			{
				rad=lens;//角度
			}
			else if (type == Rose.ANGLE2)
			{
				rad=lens * 180 / Math.PI;
			}//角度  
			for (var j:int=0; j <= count; j++)
			{
				var len:Number=radius * Math.sin(num * rad * j);
				var point:Point=Point.polar(len,rad * j);
				drawPoint(point);
			}
		}
		
		//创建简单的鱼
		public function CreatSimpleFish(Width:Number,Height:Number,eyes_Width:Number=5,Fish_rear:Number=50,n:int=4):void
		{
				
		  this.graphics.lineStyle(1,color);
		  this.graphics.moveTo(0,0);
		  this.graphics.curveTo(Width/2, Height, Width, 0);
		  this.graphics.moveTo(0,0);
		  this.graphics.curveTo(Width/2, -Height, Width, 0);
		  
		  this.graphics.drawCircle(Width/5,0,eyes_Width);
		  
		  this.graphics.moveTo(Width/4,-Height/2.7);		  
		  this.graphics.curveTo(Width/3, 0, Width/4, Height/2.7);
		  
		  
		  //创建鱼尾		  
		  this.graphics.moveTo( Width, 0);		  
		  this.graphics.lineTo( Width+Width/n,  Fish_rear);
		  
		  this.graphics.moveTo( Width, 0);
		  
		  this.graphics.lineTo( Width+Width/n,  -Fish_rear);
		  this.graphics.lineTo( Width+Width/n,  +Fish_rear);
			
		}
				
		//绘图对象，基本坐标px,py,角度，偏移长度，数量
		public function CreatTree(g:Graphics,px:Number,py:Number,angle:Number,len:Number,n:int):void
		{
			if (n > 0)
			{
				var x1:Number=px + 0.1 * len * Math.cos(angle * Math.PI / 180);
				var y1:Number=py - 0.1 * len * Math.sin(angle * Math.PI / 180);

				var x2:Number=px + len * Math.cos(angle * Math.PI / 180);
				var y2:Number=py - len * Math.sin(angle * Math.PI / 180);

				drawLine(g,n - 1,px,py,x2,y2);

				var a_left:Number=angle + 30;

				var a_right:Number=angle - 30;

				len=len * 2 / 3;
				CreatTree(g,x2,y2,angle - Math.random() * 10,len,n - 1);
				CreatTree(g,x1,y1,a_left,len * 2 / 3,n - 1);
				CreatTree(g,x1,y1,a_right,len * 2 / 3,n - 1);

				CreatTree(g,x2,y2,a_left,len * 2 / 3,n - 1);
				CreatTree(g,x2,y2,a_right,len * 2 / 3,n - 1);
			}
		}
		//绘制线
		private function drawLine(g:Graphics,n:int,x1:Number,y1:Number,x2:Number,y2):void
		{
			g.lineStyle(n,color);
			g.moveTo(x1,y1);
			g.lineTo(x2,y2);
		}
		
		//描点的办法		
		private function drawPoint(point:Point):void
		{
			this.graphics.lineTo(point.x,point.y);
		}
		
		
		//清除的办法	
		public function clear():void
		{
			this.graphics.clear();
		}
	}

}